"use strict";

//公共组件 - 菜单
ym2App.controller("ym2.ctrl.public.compmenu", ["$scope", "ym2", "layui", function ($scope, ym2, layui) {
    var topLayer = top.layer;
    var topYmSpinner = top.ymSpinner;

    //记录菜单滚动位置，重载页面时，保持滚动位置不变。
    var menuItemObject = document.querySelector("#ym2_js_menuitem"),
        MENU_CACHE_TABLE = "aside_menu",
        recordMenuScrollLocation = function () {
            window.removeEventListener("unload", recordMenuScrollLocation);
            layui.data(MENU_CACHE_TABLE, {
                key: "scrollTop",
                value: menuItemObject.scrollTop || 0,
            });
        },
        menu_scroll_location_restore = function () {
            menuItemObject.scrollTo({
                top: layui.data(MENU_CACHE_TABLE).scrollTop || 0,
                behavior: "instant",
            });
        };

    window.addEventListener("unload", recordMenuScrollLocation);
    menu_scroll_location_restore();

    /**
     * 用户偏好设置
     */
    $scope.preferenceSet = function () {
        var refererName = ym2.layerRefererWindowName( "self" );
        // var loadingFlag = topYmSpinner.show("加载中...");
        var layerCf = {
            type: 2, // iframe
            area: [ym2.layerScreenOptions("720px").width, "520px"],
            title: "偏好设置",
            shade: 0.3, // 遮罩透明度
            shadeClose: false, // 点击遮罩区域，关闭弹层
            closeBtn: 1,
            maxmin: false, // 允许全屏最小化
            anim: 0, // 0-6 的动画形式，-1 不开启
            scrollbar: false, // 打开弹层时，是否允许浏览器出现滚动条。
            content: ym2.addUrlQueryParam('/home/preference', {_t: (new Date).getTime()}),
            success: function (layerObj) {
                // topYmSpinner.hide( loadingFlag );
                top[layerObj.find("iframe")[0]["name"]].__REFERER_WIN_NAME = refererName;
            },
        };

        topLayer.open(layerCf);
    };

    /**
     * 重载页面，生效偏好
     */
    window.ym2CbkExport._preference_reload = function () {
        window.location.reload();
    };

}]);